<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div id="app">
        <!-- keep-alive可以结合路由使用也可以结合动态组件使用 -->
        <keep-alive>
            <router-view></router-view>
        </keep-alive>
    </div>
    <script src="./vue.min.js"></script>
    <script src="./router.js"></script>
    <script>
        const page1 = {
            template: `
                <div>
                    这是页面一
                    <router-link to="/page2">进入页面2</router-link>
                </div>
            `,
            created() {
                console.log('页面一创建完成')
            },
            activated() {
                console.log('页面一激活')
            },

            deactivated() {
                console.log('页面一失活')
            },
            destroyed() {
                console.log('页面一被销毁了')
            },
        }
        const page2 = {
            template: `
                <div>
                    这是页面二
                    <router-link to="/">进入页面1</router-link>
                </div>
            `,
            created() {
                console.log('页面二创建完成')
            },
            // 组件激活和组件失活的生命周期只有在使用了keep-alive的情况下才会生效
            activated() {
                console.log('页面二激活')
            },

            deactivated() {
                console.log('页面二失活')
            }
        }
        let router = new VueRouter({
            routes: [
                {
                    path: '/',
                    component: page1
                },
                {
                    path: '/page2',
                    component: page2
                }
            ]
        })
        new Vue({
            el: '#app',
            router
        })
    </script>
</body>
</html>